/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.jfonia.connect5.numerical;

import org.jfonia.connect5.basics.MutableValueNode;
import org.jfonia.connect5.relations.ABRelation;

/**
 *
 * @author Rik Bauwens
 */
public class IntMaxRelation extends ABRelation<Integer, Integer>
{
        public IntMaxRelation(MutableValueNode<Integer> a, MutableValueNode<Integer> b)
        {
                super(a, b);
        }

        @Override
        public void updateB()
        {
                if(b.getValue() > a.getValue())
                        a.setValue(b.getValue(), this);
        }

        @Override
        public void updateA()
        {
                //only update value of a when the value of b is greater then
                //the value of a (maxrelation, a is max)
                if(b.getValue() > a.getValue())
                        a.setValue(b.getValue(), this);
                //if the value of b is less than a, it's possible that the changed
                //value of b was indeed the now changed maximum, thus a new
                //maximum needs to be found; if the value of a is set to the smallest
                //value possible (min_value instead of 0 -> relation also works for
                //negative values) and all observers are notified (via setValue()),
                //this can be done in the updateB() method, which is called in the
                //changed() method in ABRelation
                else
                        a.setValue(new Integer(Integer.MIN_VALUE));
        }
}
